package com.qfedu.zf.my.controller;

import cn.hutool.core.convert.NumberWithFormat;
import com.qfedu.zf.common.config.SystemConfig;
import com.qfedu.zf.common.utils.JwtUtil;
import com.qfedu.zf.common.vo.R;
import com.qfedu.zf.my.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/my")
public class CollectController {
    @Autowired
    CollectService collectService;

    @GetMapping("/collect")
    public R collect(Integer uid, HttpServletRequest request) {
        String lctoken = request.getHeader("Lctoken");
        Object id = JwtUtil.parseToken(lctoken, "id");
        String s = id.toString();
        int i = Integer.parseInt(s);
        return collectService.collect(i);
    }

    @GetMapping("collect/find")
    public R find(@RequestParam("listingId") Integer listingId, HttpServletRequest request) {
        String header = request.getHeader(SystemConfig.HEADER_TOKEN);
        if (header == null){
            return R.fail("请先登录");
        }
        if (listingId == null){
            return R.fail("参数错误");
        }
        Integer userId = ((NumberWithFormat)JwtUtil.parseToken(header, "id")).intValue();
        return collectService.find(listingId, userId);
    }

    @GetMapping("collect/add")
    public R add(@RequestParam("listingId") Integer listingId, HttpServletRequest request) {
        String header = request.getHeader(SystemConfig.HEADER_TOKEN);
        if (header == null){
            return R.fail("请先登录");
        }
        if (listingId == null){
            return R.fail("参数错误");
        }
        Integer userId = ((NumberWithFormat)JwtUtil.parseToken(header, "id")).intValue();
        return collectService.add(listingId, userId);
    }



}
